
package com.openwater;

import java.io.IOException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;

import com.openwater.MyGestureDetector.OnActionListener;
import com.openwater.core.Quad;
import com.openwater.core.ShaderManager;
import com.openwater.effect.ParticleSystem;
import com.openwater.tools.BitmapToolkit;
import com.openwater.tools.GLTools20;
import com.openwater.tools.math.MatrixState;

public class MySurfaceView extends GLSurfaceView {

    public MySurfaceView(Context context) {
        super(context);
        setEGLContextClientVersion(2);
        setRenderer(mRenderer);
        setRenderMode(RENDERMODE_CONTINUOUSLY);
        registeTouchListener();
    }


    private final float TOUCH_SCALE_FACTOR = 180.0f / 360;
    private PointF angle = new PointF();

    private void registeTouchListener() {
        new MyGestureDetector(this).setOnActionListener(new OnActionListener() {

            @Override
            public void onActionUp(float x, float y) {

            }

            @Override
            public void onActionMove(float x, float y) {
                angle.x += x * TOUCH_SCALE_FACTOR;
                angle.y += y * TOUCH_SCALE_FACTOR;
            }

            @Override
            public void onActionFling(float velocityX, float velocityY) {

            }

            @Override
            public void onActionDown(float x, float y) {

            }
        });
    }

    private ShaderManager mShaderMgr = null;

    private Quad mQuad = null;
    private ParticleSystem mParticle = null;
    private final Renderer mRenderer = new Renderer() {

        private final String TAG = Renderer.class.toString();
        private int texid;
        private int partcleTexid;

        private void initShader() {
            mShaderMgr = ShaderManager.instance(getResources());
            mShaderMgr.genShader("particleShader", "vertex_particle.sh", "frag_particle.sh");
            Log.d(TAG, "init shader finished");
        }



        private void initObject() {
            Bitmap bitmap = BitmapToolkit.loadBitmapFromAssets(getResources(), "image1.jpg");
            mQuad = new Quad(mShaderMgr.getBaseShader(), bitmap.getWidth(), bitmap.getHeight());
            texid = GLTools20.genTexId(bitmap);
            try {
                partcleTexid = GLTools20.genTexId(getResources().getAssets().open("smoke.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mParticle = new ParticleSystem(mShaderMgr.getShader("particleShader"));
            Log.d(TAG, "init object finished");
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            initShader();
            initObject();
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
            MatrixState.setLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0);
            MatrixState.setOrtho(-width / 2.0f, width / 2.0f, -height / 2.0f, height / 2.0f, 1,
                    1000);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
            GLES20.glClearColor(0, 0, 0, 1);

            MatrixState.pushMatrix();
            MatrixState.translate(0, 0, -100);
            MatrixState.rotate(angle.y, 0, 1, 0);
            MatrixState.rotate(angle.x, 1, 0, 0);
            mParticle.drawSelf(partcleTexid);
            //MatrixState.translate(0, 0, -10);
            //mQuad.drawSelf(texid);
            MatrixState.popMatrix();
        }
    };

}
